package com.bestcem.xm.member.controller;

import com.bestcem.xm.common.core.constant.RegTypes;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.member.service.FootprintService;
import com.bestcem.xm.member.service.dto.FootprintDTO;
import com.bestcem.xm.member.service.param.AddFootprintParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.List;

/**
 * @author ming.xue <ming.xue@idiaoyan.com>
 * @desc 足迹点控制器
 * @date 2021-08-17
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/member")
public class FootprintController {
    /**
     * 足迹点服务
     */
    @Autowired
    private FootprintService footprintService;

    /**
     * 获取足迹点列表
     *
     * @param memberId
     * @return
     */
    @GetMapping("{member_id}/footprints")
    public XmResultJson<List<FootprintDTO>> listFootprints(
            @PathVariable(name = "member_id", required = false)
            @NotBlank(message = "联系人标识不能为空")
            @Pattern(regexp = RegTypes.OBJECT_ID, message = "联系人标识格式错误") String memberId,
            HttpServletRequest request) {
        
        String orgId = SecurityContextHolder.getOrgId();
        
         String userId = SecurityContextHolder.getUserId();
        if (log.isInfoEnabled()) {
            log.info("orgId: {}", orgId);
            log.info("userId: {}", userId);
        }
        ServiceResult<List<FootprintDTO>> result = footprintService.listFootprints(orgId, memberId);
        return XmResultJson.handleServiceResult(result);
    }

    /**
     * 发布足迹点
     *
     * @param param
     * @return
     */
    @PostMapping("/footprint/publish")
    public XmResultJson<String> publishFootprint(@Valid @RequestBody AddFootprintParam param) {
        ServiceResult<String> result = footprintService.publishFootprint(param);
        return XmResultJson.handleServiceResult(result);
    }

}
